package Gui;

import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.TextAnchor;

import DAO.JDBCUtil;
import DAO.*;

public class ChartTest extends ApplicationFrame
{
	       	        
    public ChartTest(String title,String course) throws SQLException
    {
        super(title);
        this.setContentPane(createPanel(course)); //构造函数中自动创建Java的panel面板
    }
    
    public static JPanel createPanel(String course) throws SQLException
    {
        JFreeChart chart =createChart(createDataset(course),course);
        return new ChartPanel(chart); //将chart对象放入Panel面板中去，ChartPanel类已继承Jpanel
    }
    
    /**
     * 
     * 用数据集创建一个图表
     */
	public static JFreeChart createChart(CategoryDataset dataset, String course) //用数据集创建一个图表
    {
        JFreeChart chart=ChartFactory.createBarChart("hi", "成绩情况", 
                "人数", dataset, PlotOrientation.VERTICAL, true, true, false); //创建一个JFreeChart
        chart.setTitle(new TextTitle(course+"成绩图",new Font("宋体",Font.BOLD+Font.ITALIC,20)));//设置主标题
        CategoryPlot plot = (CategoryPlot) chart.getCategoryPlot();
        
        
		        /**
		         * 柱状图上显示数字
		         */
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        //显示条目标签
        renderer.setBaseItemLabelsVisible(true);
        //设置条目标签生成器
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        //设置条目标签显示的位置,outline表示在条目区域外,baseline_center表示基于基线且居中
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        
        return chart;
    }
	
	/**
     * 
     * 创建数据集
     */
    public static CategoryDataset createDataset(String course) throws SQLException 
    {
    	Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        
        String sql ="select * from students";
        con = JDBCUtil.getConnection();
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
            
        /*分别存放三科成绩*/
        List<Double> jscore = new ArrayList<Double>();
        List<Double> mscore = new ArrayList<Double>();
        List<Double> escore = new ArrayList<Double>();
        
        int[] a = new int[6];  //存放Java成绩分段(A~F)
        int[] b = new int[6];  //存放Math成绩分段(A~F)
        int[] c = new int[6];  //存放Eenglish成绩分段(A~F)
        while(rs.next()){
        	double sa = rs.getDouble("javaScore");
         	jscore.add(sa);
          	judge(sa,a);
        	
         	double sb = rs.getDouble("mathScore");
           	mscore.add(sb);
           	judge(sb,b);
           	
           	double sc = rs.getDouble("englishScore");
           	escore.add(sc);
           	judge(sc,c);
               
       }
       //用户选择Java成绩柱状图
        DefaultCategoryDataset dataset0 = null;
        String choice1 = course;//随便定了一个判断条件，整合时修改
        
        
        if(choice1.equals("java"))
        	dataset0 = dataset(a);
        else if(choice1.equals("math")) 
        	dataset0 = dataset(b);  //选择数学成绩柱状图
        else if(choice1.equals("english"))
        	dataset0 = dataset(c);  //选择英语成绩柱状图
        JDBCUtil.realeaseAll(rs, ps, con);
        return dataset0;
        
        
    }
    
    
    /**
     * 
     * 数据与图表位置对应
     */
    private static DefaultCategoryDataset dataset(int[] arr) {
    	DefaultCategoryDataset dataset=new DefaultCategoryDataset();
        dataset.setValue(arr[0],"A","0~50");
        dataset.setValue(arr[1],"B","50~60");
        dataset.setValue(arr[2],"C","60~70");
        dataset.setValue(arr[3],"D","70~80");
        dataset.setValue(arr[4],"E","80~90");
        dataset.setValue(arr[5],"F","90~100");
        return dataset;
		
	}
    
    /**
     * 
     * 将成绩分段
     */
	private static void judge(double score, int[] arr) {
		if(score>=0&&score<50)
			arr[0]++;
		else if(score>=50&&score<60)
			arr[1]++;
		else if(score>=60&&score<70)
			arr[2]++;
		else if(score>=70&&score<80)
			arr[3]++;
		else if(score>=80&&score<90)
			arr[4]++;
		else if(score>=90&&score<=100)
			arr[5]++;
	}
}